// pages/mall/remark/index.js
const $api = require('../../../../request/api').API;
import Dialog from '../../../../miniprogram_npm/@vant/weapp/dialog/dialog';
import { recordSoundCpn } from '../../../../utils/util';
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showSoundCop: false,
    remark: '',
    recordRemark: '',
    showRecord: false,
    beeperId: '',
    edit: false,
    tabList: [
      { id: 1, title: '不吃辣椒' },
      { id: 2, title: '多放点糖' },
      { id: 3, title: '汤和粉分开' }
    ],
    eventId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.beeperId) {
      this.setData({
        beeperId: options.beeperId
      });
    }
    this.setData({
      eventId: app.globalData['btnList.eventId']
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.remark) {
      this.setData({
        remark: app.globalData.remark
      });
    }
    if (app.globalData.recordRemark) {
      this.setData({
        recordRemark: app.globalData.recordRemark
      });
    }
  },

  //  输入备注的值改变
  changeInput(e) {
    this.setData({
      remark: e.detail
    });
  },
  saveRecord(e) {
    if (e.detail) {
      this.setData({
        recordRemark: e.detail.file
      });
    }
  },
  // 编辑或保存
  editOrSave() {
    this.setData({
      edit: !this.data.edit
    });
  },
  // 添加备注
  addRemark(e) {
    if (this.data.edit) return;
    this.setData({
      remark: this.data.remark + e.currentTarget.dataset.title
    });
  },
  // 清除标签
  clearTab(e) {
    this.data.tabList.splice(e.currentTarget.dataset.index, 1);
    this.setData({
      tabList: this.data.tabList
    });
  },
  recordSound() {
    this.selectComponent('#sound-record-two').start();
  },
  endSound() {
    this.selectComponent('#sound-record-two').end();
  },
  // 取消录音备注
  cancelRecordRemark() {
    wx.showToast({
      title: '已取消录音备注'
    });
    this.setData({
      recordRemark: ''
    });
  },
  // 完成
  finish() {
    Dialog.confirm({
      title: '提示',
      message: '确认保存备注?'
    })
      .then(res => {
        app.globalData.remark = this.data.remark;
        app.globalData.recordRemark = this.data.recordRemark;
        app.globalData.saveRemark = true;
        wx.navigateBack();
      })
      .catch(err => {});
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    recordSoundCpn();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    recordSoundCpn();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
});
